/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation 
 * 
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 * 
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Crypto Component                                                 */
/**                                                                       */
/**   Crypto Self Test                                                    */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_CRYPTO_SOURCE_CODE


/* Include necessary system files.  */
#include "nx_crypto_method_self_test.h"
#include "nx_crypto_pkcs1_v1.5.h"

#ifdef NX_CRYPTO_SELF_TEST

static UCHAR pub_e_2048[] = {0x01, 0x00, 0x01};

static UCHAR pri_e_2048[] = {
0x13, 0xFF, 0x74, 0x29, 0xF8, 0xE8, 0x51, 0xF1, 0x07, 0x9C, 0xCF, 0xCE, 0x3B, 0x3C, 0xD8, 0x60, 
0x6A, 0xBA, 0x86, 0x07, 0xAD, 0x85, 0xCB, 0xB3, 0x05, 0x75, 0x01, 0xEB, 0xD5, 0x88, 0x11, 0xF3, 
0xC0, 0x48, 0x23, 0x17, 0x1F, 0x19, 0x2C, 0x04, 0x8E, 0x1E, 0x88, 0x3A, 0xF8, 0xCF, 0x95, 0x88, 
0x10, 0x15, 0x1D, 0x38, 0x74, 0xAE, 0xDC, 0x8E, 0xC4, 0xF8, 0x8D, 0x20, 0x65, 0xC5, 0x81, 0x56, 
0x9F, 0x1E, 0x20, 0x08, 0x52, 0xDD, 0x40, 0xB6, 0xDF, 0xD1, 0x65, 0x26, 0x59, 0x08, 0x5A, 0x9D, 
0xD1, 0xD3, 0xB8, 0x69, 0xEA, 0x36, 0x17, 0xD9, 0x04, 0xD2, 0x09, 0xDE, 0x15, 0x6A, 0x60, 0xBA, 
0x59, 0x29, 0xD0, 0x2F, 0x16, 0x43, 0x02, 0x73, 0xD1, 0x07, 0x20, 0xC2, 0xF2, 0x8D, 0x2B, 0x95, 
0x68, 0x4D, 0xCA, 0xA6, 0xB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, 0x9f, 0x91, 0x74, 0x2a, 0x20, 0x74, 0x64, 0xb1, 0xd0, 0x25, 0xd3, 0x6b,
0xd5, 0xeb, 0x35, 0x89, 0x51, 0x07, 0xe9, 0x77, 0x2e, 0xd1, 0x45, 0xc3, 0xcf, 0xc9, 0x81, 0xd9,
0xd3, 0xf9, 0xa3, 0xb5, 0x7c, 0x9a, 0x9f, 0x43, 0xc4, 0xde, 0xeb, 0x08, 0x2a, 0x83, 0x64, 0x32,
0x25, 0xf3, 0x63, 0xea, 0xc8, 0xe6, 0xe0, 0x00, 0x8c, 0x1b, 0x64, 0xfa, 0x4d, 0xb6, 0xd2, 0x0d
};

static UCHAR output[512];

extern NX_CRYPTO_METHOD crypto_method_rsa;
extern NX_CRYPTO_METHOD crypto_method_sha256;

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    nx_crypto_method_self_test_pkcs1                    PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function performs the Known Answer Test for PKCS1#v1.5 crypto  */
/*    method.                                                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    method_ptr                            Pointer to the crypto method  */
/*                                            to be tested.               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
/*  09-30-2020     Timothy Stapko           Modified comment(s),          */
/*                                            resulting in version 6.1    */
/*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
/*                                            renamed FIPS symbol to      */
/*                                            self-test,                  */
/*                                            resulting in version 6.1.7  */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_pkcs1(NX_CRYPTO_METHOD *crypto_method_pkcs1,
                                                      VOID *metadata, UINT metadata_size)
{
NX_CRYPTO_PKCS1_OPTIONS pkcs1_options;
UCHAR *modulus;
UINT   modulus_len;
UCHAR *pub_e;
UINT   pub_e_len;
UCHAR *pri_e;
UINT   pri_e_len;
UCHAR *plain_text;
UINT   input_length;
UCHAR *cipher_text;
UINT   status;


    /* Validate the crypto method */
    if(crypto_method_pkcs1 == NX_CRYPTO_NULL)
        return(NX_CRYPTO_PTR_ERROR);

    /* Set the test data.  */
    pkcs1_options.public_cipher_method = &crypto_method_rsa;
    pkcs1_options.public_cipher_metadata = (UCHAR *)metadata + sizeof(NX_CRYPTO_PKCS1);
    pkcs1_options.public_cipher_metadata_size = crypto_method_rsa.nx_crypto_metadata_area_size;
    pkcs1_options.hash_method = &crypto_method_sha256;
    pkcs1_options.hash_metadata = (UCHAR *)pkcs1_options.public_cipher_metadata + sizeof(NX_CRYPTO_RSA);
    pkcs1_options.hash_metadata_size = crypto_method_sha256.nx_crypto_metadata_area_size;

    modulus = m_2048;
    modulus_len = sizeof(m_2048);
    pub_e = pub_e_2048;
    pub_e_len = sizeof(pub_e_2048);
    pri_e = pri_e_2048;
    pri_e_len = sizeof(pri_e_2048);
    plain_text = msg;
    input_length = sizeof(msg);
    cipher_text = s;

    /* Clear the output buffer.  */
    NX_CRYPTO_MEMSET(output, 0, sizeof(output));

    /* Call the crypto initialization function.  */
    if (crypto_method_pkcs1 -> nx_crypto_init)
    {
        status = crypto_method_pkcs1 -> nx_crypto_init(crypto_method_pkcs1,
                                                       NX_CRYPTO_NULL,
                                                       0,
                                                       NX_CRYPTO_NULL,
                                                       metadata,
                                                       metadata_size);

        if (status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }
    }

    if (crypto_method_pkcs1 -> nx_crypto_operation == NX_CRYPTO_NULL)
    {
        return(NX_CRYPTO_PTR_ERROR);
    }

    /* Call the crypto operation function.  */
    status = crypto_method_pkcs1 -> nx_crypto_operation(NX_CRYPTO_SET_ADDITIONAL_DATA,
                                                        NX_CRYPTO_NULL,
                                                        crypto_method_pkcs1,
                                                        modulus,
                                                        modulus_len << 3,
                                                        (UCHAR *)&pkcs1_options,
                                                        sizeof(pkcs1_options),
                                                        NX_CRYPTO_NULL,
                                                        NX_CRYPTO_NULL,
                                                        0,
                                                        metadata,
                                                        metadata_size,
                                                        NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    if (status != NX_CRYPTO_SUCCESS)
    {
        return(status);
    }

    status = crypto_method_pkcs1 -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
                                                        NX_CRYPTO_NULL,
                                                        crypto_method_pkcs1,
                                                        pri_e,
                                                        pri_e_len << 3,
                                                        plain_text,
                                                        input_length,
                                                        NX_CRYPTO_NULL,
                                                        output,
                                                        modulus_len,
                                                        metadata,
                                                        metadata_size,
                                                        NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    if (status != NX_CRYPTO_SUCCESS)
    {
        return(status);
    }

    /* Validate the output.  */
    if(NX_CRYPTO_MEMCMP(output, cipher_text, modulus_len) != 0)
    {
        return(NX_CRYPTO_NOT_SUCCESSFUL);
    }

    status = crypto_method_pkcs1 -> nx_crypto_operation(NX_CRYPTO_VERIFY,
                                                        NX_CRYPTO_NULL,
                                                        crypto_method_pkcs1,
                                                        pub_e,
                                                        pub_e_len << 3,
                                                        plain_text,
                                                        input_length,
                                                        NX_CRYPTO_NULL,
                                                        cipher_text,
                                                        modulus_len,
                                                        metadata,
                                                        metadata_size,
                                                        NX_CRYPTO_NULL, NX_CRYPTO_NULL);

    /* Check the status.  */
    if(status != NX_CRYPTO_SUCCESS)
    {
        return(status);
    }

    if (crypto_method_pkcs1 -> nx_crypto_cleanup)
    {
        status = crypto_method_pkcs1 -> nx_crypto_cleanup(metadata);
    }

    return(status);
}
#endif
